package com.example.lottery_system.service.impl;

import com.example.lottery_system.common.errcode.ServiceErrorCodeConstant;
import com.example.lottery_system.common.exception.ServiceException;
import com.example.lottery_system.common.utils.*;
import com.example.lottery_system.service.VerificationCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class VerificationCodeServiceImpl implements VerificationCodeService {
    /**
     * 发送验证码
     * @param phoneNumber
     */

//    模板CODE
    private static final String TEMPLATE_CODE = "SMS_478585712";

//    验证码保存时间
    private static  final long OUT_TIME_MINUTE = 1;

    // 设置验证码的描述key
    private static  final String CAPTCHA_KEY_PRE = "CAPTCHA_";
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private SMSUtil smsUtil;

    @Autowired
    private MailUtil mailUtil;
    Map<String,String> templateParam = new HashMap<>();

    @Override
    public void sendVerificationCodeByPhone(String phoneNumber) {
        /**
         * 1. 校验手机号格式
         */
        if (!StringUtils.hasLength(phoneNumber)) {
            throw new ServiceException(ServiceErrorCodeConstant.RECAPTCHA_CODE_PHONE_ISNULL);
        }
        if (!RegexUtil.checkMobile(phoneNumber)) {
            throw new ServiceException(ServiceErrorCodeConstant.REGISTER_PHONE_FORMAT_ERROR);
        }

        /**
         * 2. 创建随机验证码
         */
        String code = ReCaptchaUtil.getCaptcha();


        /**
         * 3.发送验证码
         */

        templateParam.put("code",code);
        templateParam.put("num",OUT_TIME_MINUTE + "");

        smsUtil.sendMessage(TEMPLATE_CODE,phoneNumber, JacksonUtil.writeValueAsString(templateParam));
        log.info("手机号发送成功的验证码为:{} " , code);
        /**
         * 4.存储到 Redis 中并设置超时时间为一分钟
         */
        redisUtil.put(CAPTCHA_KEY_PRE+ phoneNumber,code,OUT_TIME_MINUTE);

    }

    /**
     * 获取验证码
     * @param phoneNumber
     * @return
     */
    @Override
    public String getVerificationCodeFromPhone(String phoneNumber) {
        /**
         * 1. 校验手机号格式
         */
        if (!StringUtils.hasLength(phoneNumber)) {
            throw new ServiceException(ServiceErrorCodeConstant.RECAPTCHA_CODE_PHONE_ISNULL);
        }
        if (!RegexUtil.checkMobile(phoneNumber)) {
            throw new ServiceException(ServiceErrorCodeConstant.REGISTER_PHONE_FORMAT_ERROR);
        }


////        如果验证码存在就返回
        if (redisUtil.contains(CAPTCHA_KEY_PRE + phoneNumber)) {
            return redisUtil.get(CAPTCHA_KEY_PRE + phoneNumber);
        } else {
//            不存在就抛出异常
            throw  new ServiceException(ServiceErrorCodeConstant.RECAPTCHA_CODE_OUT_TIME);
        }
    }

    @Override
    public void sendVerificationCodeByEmail(String email) {
        /**
         * 校验邮箱格式
         */
        if (!StringUtils.hasText(email)) {
            throw new ServiceException(ServiceErrorCodeConstant.RECAPTCHA_CODE_EMAIL_ISNULL);
        }

        if (!RegexUtil.checkMail(email)) {
            throw  new ServiceException(ServiceErrorCodeConstant.REGISTER_MAIL_FORMAT_ERROR);
        }

        /**
         * 2. 随机创建验证码
         */
        String code = ReCaptchaUtil.getCaptcha();

        /**
         * 3. 发送验证码到邮箱
         */
        String title = "登录验证码";
        String context = "尊贵的彦祖/热巴您好！你本次登录的验证码为: "
                + code + "。验证码在" + OUT_TIME_MINUTE + "分钟之内有效,请勿泄露给他人哦~";
        mailUtil.sendSampleMail(email,title,context);
        log.info("邮箱发送成功的验证码为:{} " , code);

        /**
         * 4. 将验证码存入缓存中
         */
        String key = CAPTCHA_KEY_PRE + email;
        redisUtil.put(key,code,OUT_TIME_MINUTE);
    }

    @Override
    public String getVerificationCodeFromEmail(String email) {
//        校验
        if (!StringUtils.hasText(email)) {
            throw new ServiceException(ServiceErrorCodeConstant.RECAPTCHA_CODE_EMAIL_ISNULL);
        }

        if (!RegexUtil.checkMail(email)) {
            throw  new ServiceException(ServiceErrorCodeConstant.REGISTER_MAIL_FORMAT_ERROR);
        }

//        从缓存中直接返回
        String key = CAPTCHA_KEY_PRE + email;
        if (redisUtil.contains(key)) {
            return redisUtil.get(key);
        } else {
            //   不存在就抛出异常
//            一般正常情况下为超时了!
            throw  new ServiceException(ServiceErrorCodeConstant.RECAPTCHA_CODE_OUT_TIME);
        }
    }
}
